
import SwiftUI

struct DataPoint: Identifiable {
    let id = UUID()
    let value: CGFloat
    let label: String
}

struct SportLineChartView: View {
    let data: [DataPoint]
    let lineColor: Color
    let lineWidth: CGFloat
    let showPoints: Bool
    let pointSize: CGFloat
    
    private var maxYValue: CGFloat {
        (data.map { $0.value }.max() ?? 0) * 1.2 // 留20%顶部空间
    }
    
    var body: some View {
        VStack(alignment: .leading) {
            HStack(spacing: 0) {
//                // Y轴和刻度
//                YAxisView(maxValue: maxYValue)
//                    .frame(width: 30)
                
                // 主图表区域
                GeometryReader { geometry in
                    ZStack {
                        // 水平网格线
                        HorizontalGridLines(maxValue: maxYValue)
                            .stroke(Color.gray.opacity(0.2), lineWidth: 1)
                        
                        // 曲线
                        LineShape(dataPoints: data, maxValue: maxYValue)
                            .stroke(lineColor, style: StrokeStyle(
                                lineWidth: lineWidth,
                                lineCap: .round,
                                lineJoin: .round
                            ))
                        
                        // 数据点
                        if showPoints {
                            PointsView(
                                dataPoints: data,
                                maxValue: maxYValue,
                                pointSize: pointSize,
                                color: lineColor
                            )
                        }
                    }
                }
            }
            
            // X轴和刻度
            XAxisView(dataPoints: data)
                .frame(height: 20)
        }
        .padding()
    }
}

// 曲线绘制
struct LineShape: Shape {
    let dataPoints: [DataPoint]
    let maxValue: CGFloat
    
    func path(in rect: CGRect) -> Path {
        Path { path in
            guard dataPoints.count > 1 else { return }
            
            let stepX = rect.width / CGFloat(dataPoints.count - 1)
            let stepY = rect.height / maxValue
            
            let firstPoint = CGPoint(
                x: 0,
                y: rect.height - (dataPoints[0].value * stepY)
            )
            path.move(to: firstPoint)
            
            for i in 1..<dataPoints.count {
                let point = CGPoint(
                    x: stepX * CGFloat(i),
                    y: rect.height - (dataPoints[i].value * stepY)
                )
                path.addLine(to: point)
            }
        }
    }
}

// Y轴视图
struct YAxisView: View {
    let maxValue: CGFloat
    let divisions: Int = 5
    
    var body: some View {
        GeometryReader { geometry in
            ZStack(alignment: .trailing) {
                // 轴线
                Path { path in
                    path.move(to: CGPoint(x: geometry.size.width, y: 0))
                    path.addLine(to: CGPoint(x: geometry.size.width, y: geometry.size.height))
                }
                .stroke(Color.gray, lineWidth: 1)
                
                // 刻度标签
                ForEach(0..<divisions, id: \.self) { i in
                    let yValue = maxValue / CGFloat(divisions - 1) * CGFloat(i)
                    let yPos = geometry.size.height - (geometry.size.height / CGFloat(divisions - 1) * CGFloat(i))
                    
                    Text(String(format: "%.1f", yValue))
                        .font(.system(size: 8))
                        .position(x: geometry.size.width - 15, y: yPos)
                }
            }
        }
    }
}

// X轴视图
struct XAxisView: View {
    let dataPoints: [DataPoint]
    
    var body: some View {
        GeometryReader { geometry in
            ZStack(alignment: .top) {
                // 轴线
//                Path { path in
//                    path.move(to: CGPoint(x: 0, y: 0))
//                    path.addLine(to: CGPoint(x: geometry.size.width, y: 0))
//                }
//                .stroke(Color.gray, lineWidth: 1)
                
                // 刻度标签
                ForEach(dataPoints.indices, id: \.self) { i in
                    if i % 1 == 0 { // 间隔显示标签
                        let xPos = (geometry.size.width / CGFloat(dataPoints.count-1))
                        * CGFloat(i)
                        Text(dataPoints[i].label)
                            .font(.system(size: 12))
                            .foregroundStyle(.white)
//                            .rotationEffect(.degrees(-45))
                            .offset(y: 10)
                            .frame(width: 40)
                            .position(x: xPos, y: 0)
                    }
                }
            }
        }
    }
}

// 水平网格线
struct HorizontalGridLines: Shape {
    let maxValue: CGFloat
    let divisions: Int = 6
    
    func path(in rect: CGRect) -> Path {
        Path { path in
            for i in 0..<divisions {
                let y = rect.height - (rect.height / CGFloat(divisions - 1) * CGFloat(i))
                path.move(to: CGPoint(x: 0, y: y))
                path.addLine(to: CGPoint(x: rect.width, y: y))
            }
        }
    }
}

// 数据点视图
struct PointsView: View {
    let dataPoints: [DataPoint]
    let maxValue: CGFloat
    let pointSize: CGFloat
    let color: Color
    
    var body: some View {
        GeometryReader { geometry in
            ForEach(dataPoints) { point in
                let index = CGFloat(dataPoints.firstIndex { $0.id == point.id } ?? 0)
                let xPos = (geometry.size.width / CGFloat(dataPoints.count - 1)) * index
                let yPos = geometry.size.height - (point.value / maxValue * geometry.size.height)
                
                Circle()
                    .fill(color)
                    .frame(width: pointSize, height: pointSize)
                    .position(x: xPos, y: yPos)
            }
        }
    }
}
